/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package POJO;

import java.sql.Connection;
import DB.DBInfo;
import DB.TripManager;

/**
 *
 * @author kristof
 */
public class Maps {

    public Maps() {
    }

    public String Map(User user, Connection c) {

        String output = "";
        ContainerObject co[] = null;

        /*output = "<script type=\"text/javascript\">alert(\"" + user.username + "\")" +              "</script>";
        if (user != null && user.username!= null && c != null) {
        co= JavascriptHandler.GetContainersForTrip (2, user, c);
        }
        if (co != null && co[0] != null) {
        output = "<script type=\"text/javascript\">alert(\"" + String.valueOf(co[0].east) + "\")" +              "</script>";
        }*/

        output += "<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAIdzBHGBZLJ40eVvLvr9goxR_DG_liGIkEeuv8JQWC2MPpHxNPBQ2HrQkABkFPyA91XcW1cEh_DDW9Q\"\ntype=\"text/javascript\">" +
                "</script>";
        output += "<script type=\"text/javascript\">\n";
        output += "//<![CDATA[\n";
        output += " function initialize() {\n" +
                "if (GBrowserIsCompatible()) { \n" +
                "var opts = {showOnLoad: false};var done;" +
                "var map = new GMap2(document.getElementById(\"map_canvas\"), {googleBarOptions: opts});\n" +
                "map.enableDoubleClickZoom();" +
                "map.enableScrollWheelZoom(); " +
                "map.enableContinuousZoom();" +
                "map.setCenter(new GLatLng(30, -10), 2);" +
                "map.addControl(new GLargeMapControl());\n" +
                "map.addControl(new GMapTypeControl());\n" +
                "map.enableGoogleBar();\n" +
                "var marker; var point;";
        /*output += "var marker = new GMarker (new GLatLng (20, 20)); \n" + 
        //"marker.bindInfoWindow('<div style=\"width:450; height: 400;\"><object width=\"425\" height=\"355\"><param name=\"movie\" value=\"http://www.youtube.com/v/2A-fuFeRekE&rel=1\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/2A-fuFeRekE&rel=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"></embed></object></div>');\n" +
        "marker.bindInfoWindow('<img src = \"http://media.tech-recipes.com/rx_images/google_maps_size.gif\" />');\n" +
        "map.addOverlay (marker)\n" ;*/

        TripManager tm = new TripManager(c);

        int[] everybodyTrips;
        if (user != null) {
            everybodyTrips = tm.getTripsForEverybody(user.username);
        } else {
            everybodyTrips = tm.getTripsForEverybody(null);
        }
        int[] relationTrips = null;
        int[] ownerTrips = null;
        int[] trips = null;
        if (user != null && user.username != null) {
            relationTrips = tm.getTripsForRelations(user.username);
            ownerTrips = tm.getTripsOwnedBy(user.username);
        }

        if (everybodyTrips == null) {
            everybodyTrips = new int[0];
        }
        if (relationTrips == null) {
            relationTrips = new int[0];
        }
        if (ownerTrips == null) {
            ownerTrips = new int[0];
        }
        System.out.println(relationTrips.length);
        trips = new int[everybodyTrips.length + relationTrips.length + ownerTrips.length];
        System.arraycopy(everybodyTrips, 0, trips, 0, everybodyTrips.length);
        System.arraycopy(relationTrips, 0, trips, everybodyTrips.length, relationTrips.length);
        System.arraycopy(ownerTrips, 0, trips, everybodyTrips.length + relationTrips.length, ownerTrips.length);


        //System.out.println(trips);
        for (int i = 0; trips != null && i < trips.length; i++) {

            co = JavascriptHandler.GetContainersForTrip(trips[i], user, c);
            for (int j = 0; co != null && j < co.length; j++) {


                output += "marker = new GMarker (new GLatLng (" + String.valueOf(co[j].north) + "," + String.valueOf(co[j].east) + "));\n";
                output += "marker.bindInfoWindow('<div class=\"nice\" style=\"text-align:center;\">" +
                        "<a class=\"nice\" style=\"padding: 0px;\" href = \"index.jsp?page=triplocation&containerid=" + String.valueOf(co[j].id) + "\">" + co[j].location + "</a><br/>" +
                        "in trip: <a class=\"nice\" style=\"padding: 0px;\" href = \"index.jsp?page=trip&tripid=" + trips[i] + "\">" + tm.getTripName(trips[i]) + "</a></div>');\n";
                output += "map.addOverlay (marker)\n";
            }
        }


        output += "}\n" +
                "  } \n" +
                "//]]>" +
                "</script>";

        return output;



    }

    public String selectStop(int trip, User user, Connection c) {
        String output = "";
        ContainerObject co[] = null;

        output += "<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAIdzBHGBZLJ40eVvLvr9goxR_DG_liGIkEeuv8JQWC2MPpHxNPBQ2HrQkABkFPyA91XcW1cEh_DDW9Q\"\ntype=\"text/javascript\">" +
                "</script>";
        output += "<script src= \"greversegeocoder.js\" type=\"text/javascript\"></script>";
        output += "<script type=\"text/javascript\">\n";
        output += "//<![CDATA[\n";
        output += " function selectInitialize() {\n" +
                "if (GBrowserIsCompatible()) { \n" +
                "var opts = {showOnLoad: false};" +
                "var map = new GMap2(document.getElementById(\"select_canvas\"), {googleBarOptions: opts});\n" +
                "map.enableDoubleClickZoom();" +
                "map.enableScrollWheelZoom(); " +
                "map.enableContinuousZoom();" +
                "function badresult() {\n" +
                "document.getElementById(\"location\").value = \"Unknown location\";\n" +
                "document.getElementById(\"location\").disabled=false;}\n" +
                "function goodresult(placemark) {\n" +
                "document.getElementById(\"location\").value = rg.getPlacemarkProperty(placemark,\"LocalityName\");\n" +
                /*"document.getElementById(\"location\").disabled=true;*/ "\n" +
                /* "if (document.getElementById(\"location\").value == \"\") {\n" + 
                "document.getElementById(\"location\").value = \"Unknown location\";\n" + 
                "document.getElementById(\"location\").disabled=false;}*/ "}\n" +
                "var rg = new GReverseGeocoder(map);" +
                "GEvent.addListener(rg, \"load\", goodresult);" +
                "GEvent.addListener(rg, \"error\", badresult);" +
                "map.setCenter(new GLatLng(30, -10), 1);" +
                "map.addControl(new GSmallMapControl());\n" +
                "map.enableGoogleBar();\n" +
                "var baseIcon = new GIcon();" +
                "baseIcon.shadow = \"http://www.google.com/mapfiles/shadow50.png\";" +
                "baseIcon.iconSize = new GSize(20, 34);" +
                "baseIcon.shadowSize = new GSize(37, 34);" +
                "baseIcon.iconAnchor = new GPoint(9, 34);" +
                "baseIcon.infoWindowAnchor = new GPoint(9, 2);" +
                "baseIcon.infoShadowAnchor = new GPoint(18, 25);" +
                "var greenMarker = new GIcon(baseIcon);" +
                "greenMarker.image = \"images/greenmarker.png\";" +
                "var picker = new GMarker(map.getCenter(), {draggable: false, icon:greenMarker});" +
                "GEvent.addListener(picker, \"dragend\", function() {" +
                "document.getElementById(\"north\").value = picker.getLatLng().lat();" +
                "document.getElementById(\"east\").value = picker.getLatLng().lng();});" +
                "var myEventListener = GEvent.bind(map, \"click\", this, function(marker,latlng) {" +
                "if (latlng) {" +
                "picker.setLatLng(latlng);" +
                "document.getElementById(\"north\").value = picker.getLatLng().lat();" +
                "document.getElementById(\"east\").value = picker.getLatLng().lng();" +
                "point = new GLatLng ( picker.getLatLng().lat() , picker.getLatLng().lng());\n" +
                "rg.reverseGeocode(point);" +
                "} " +
                "}); " +
                "map.addOverlay(picker);" +
                "var marker;";

        co = JavascriptHandler.GetContainersForTrip(trip, user, c);
        for (int i = 0; co != null && i < co.length; i++) {
            output += "marker = new GMarker (new GLatLng (" + String.valueOf(co[i].north) + "," + String.valueOf(co[i].east) + "));\n";
            //output += "marker.bindInfoWindow('<a href = index.jsp?page=triplocation&containerid=" + String.valueOf(co[i].id) + "');\n";
            output += "map.addOverlay (marker)\n";
        }

        output += "}\n" +
                "  } \n" +
                "//]]>" +
                "</script>";

        return output;
    }

    public String tripMap(int trip, User user, boolean userIsAdmin) {

        String output = "";
        ContainerObject co[] = null;

        output += "<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAIdzBHGBZLJ40eVvLvr9goxR_DG_liGIkEeuv8JQWC2MPpHxNPBQ2HrQkABkFPyA91XcW1cEh_DDW9Q\"\ntype=\"text/javascript\">" +
                "</script>";
        output += "<script type=\"text/javascript\">\n";
        output += "//<![CDATA[\n";
        output += " function initialize() {\n" +
                "if (GBrowserIsCompatible()) { \n" +
                "var opts = {showOnLoad: false};var done;" +
                "var map = new GMap2(document.getElementById(\"map_canvas\"), {googleBarOptions: opts});\n" +
                "map.enableDoubleClickZoom();" +
                "map.enableScrollWheelZoom(); " +
                "map.enableContinuousZoom();" +
                "map.setCenter(new GLatLng(30, -10), 1);" +
                "map.addControl(new GSmallMapControl());\n" +
                "map.addControl(new GMapTypeControl());\n" +
                "map.enableGoogleBar();\n" +
                "var marker; var point;";
        co = JavascriptHandler.GetContainersForTrip(trip, user, DBInfo.getConnection());
        for (int j = 0; co != null && j < co.length; j++) {


            output += "marker = new GMarker (new GLatLng (" + String.valueOf(co[j].north) + "," + String.valueOf(co[j].east) + "));\n";
            output += "marker.bindInfoWindow('<div class=\"nice\" style=\"text-align:center;\">" +
                    "<a class=\"nice\" style=\"padding: 0px;\" href = \"index.jsp?page=triplocation&containerid=" + String.valueOf(co[j].id) + "\">" + co[j].location + "</a><br/>";
            if (userIsAdmin) {
                output += "Click <a class=\"nice\" style=\"padding: 0px;\" href=\"index.jsp?page=trip&tripid=" + co[j].tripID + "&removeid=" + String.valueOf(co[j].id) + "\">here</a> to remove this stop.";
            }
            output += "</div>');\nmap.addOverlay (marker)\n";
        }



        output += "}\n" +
                "  } \n" +
                "//]]>" +
                "</script>";

        return output;



    }
}
